package com.dragee.modules.work.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.common.PageUtils;
import com.dragee.common.R;
import com.dragee.config.exception.BusinessException;
import com.dragee.modules.sys.entity.LoginUser;
import com.dragee.modules.sys.entity.UserEntity;
import com.dragee.modules.sys.service.UserService;
import com.dragee.modules.work.entity.*;
import com.dragee.modules.work.service.*;
import com.dragee.utils.LoginUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;



/**
 * 队伍表
 *
 * @author tc
 * @email tc@mail.com
 * @date 2023-04-19 09:42:02
 */
@RestController
@RequestMapping("work/team")
@Api(tags = "work - 队伍")
public class TeamController {
    @Autowired
    private TeamService teamService;
    @Autowired
    private UserService userService;
    @Autowired
    private TeamAthleteService teamAthleteService;
    @Autowired
    private TeamCoachService teamCoachService;
    @Autowired
    private TeamLeaderService teamLeaderService;
    @Autowired
    private TeamOtherService teamOtherService;
    @Autowired
    private TeamRefereeService teamRefereeService;

    /**
     * 列表
     */
    @GetMapping("list")
    @ApiOperation("队伍表列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = teamService.queryPage(params);
        return R.ok().put("data", page);
    }

    /**
     * 信息
     */
    @GetMapping("getLoginUserTeam")
    @ApiOperation("获取当前登录人的队伍")
    public R getLoginUserTeam() {
        TeamEntity team = teamService.getTeamByUserId(LoginUserUtil.getLoginUserId().toString());
        return R.ok().put("data", team);
    }



    /**
     * 信息
     */
    @GetMapping("{id}")
    @ApiOperation("队伍表信息")
    public R info(@PathVariable("id") Long id){
		TeamEntity team = teamService.getById(id);
        return R.ok().put("data", team);
    }

    /**
     * 保存
     */
    @PostMapping("saveOrUpdate")
    @ApiOperation("保存或修改队伍表")
    public R saveOrUpdate(@RequestBody TeamEntity team){
        LoginUser l = LoginUserUtil.getLoginUser();
        UserEntity user = userService.getById(l);
        team.setProvince(user.getProvince());
        teamService.saveOrUpdate(team);
        return R.ok();
    }


    /**
     * 删除
     */
    @GetMapping("delete-by-id")
    @ApiOperation("队伍表删除")
    public R delete( String id){
        deleteCompetitionData(id);
		teamService.removeById(Long.parseLong(id));
        return R.ok();
    }

    @GetMapping("delete-by-ids")
    @ApiOperation("队伍表批量删除")
    public R deletes(String ids){
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            deleteCompetitionData(id);
            teamService.removeById(Long.parseLong(id));
        }
        return R.ok();
    }



    private void deleteCompetitionData (String id) {
        int total = teamAthleteService.count(new QueryWrapper<TeamAthleteEntity>().eq("teamkey", id));
        if (total > 0) {
            throw new BusinessException("该数据下存在运动员数据,请先删除该数据");
        }
        total = teamCoachService.count(new QueryWrapper<TeamCoachEntity>().eq("teamkey", id));
        if (total > 0) {
            throw new BusinessException("该数据下存在教练数据,请先删除该数据");
        }
        total = teamLeaderService.count(new QueryWrapper<TeamLeaderEntity>().eq("teamkey", id));
        if (total > 0) {
            throw new BusinessException("该数据下存在领队数据,请先删除该数据");
        }
        total = teamOtherService.count(new QueryWrapper<TeamOtherEntity>().eq("teamkey", id));
        if (total > 0) {
            throw new BusinessException("该数据下存在随队人员数据,请先删除该数据");
        }
        total = teamRefereeService.count(new QueryWrapper<TeamRefereeEntity>().eq("teamkey", id));
        if (total > 0) {
            throw new BusinessException("该数据下存在裁判数据,请先删除该数据");
        }

    }

}
